package com.etc.controller;


import com.etc.pojo.Cake;
import com.etc.service.CakeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

/**
 * @author 杰哥
 * @Title: CakeController
 * @ProjectName myssm
 * @Description: TODO
 * @date 2019/8/211:59
 */
@Controller
@RequestMapping("cake")
public class CakeController {

    @Autowired
    private CakeService cakeService;

    @RequestMapping("index")
    public String index(){
        return "index";
    }

    /**
     * 查询全部
     * @return
     */
    @RequestMapping("select")
    public ModelAndView select(){
        List<Cake> select = cakeService.select();
        ModelAndView mv = new ModelAndView("cake");
        mv.addObject("cakeList", select);
        return mv;
    }

    /**
     *
     *添加商品
     * out 1 成功 0失败
     */
    @RequestMapping(value = "insert" ,method = RequestMethod.POST)
    public void insert(
            @RequestParam (value = "cname") String cname,
            @RequestParam (value = "price") int price,
            @RequestParam (value = "quantity") int quantity,
            @RequestParam (value = "blessing") String blessing,
            @RequestParam (value = "image") String image,
            @RequestParam (value = "shopId")int shopId,
            PrintWriter out){
        Integer insert = cakeService.insert(new Cake(cname,price,quantity,blessing,image,shopId));
        if(insert == 1){
            out.print(1);

        }else {
            out.print(0);
        }
    }

    @RequestMapping("delete")
    public void delete(@RequestParam (value = "cid") int cid,PrintWriter out){
        Integer delete = cakeService.deleteById(cid);
        if(delete == 1){
            out.print(1);
        }else{
            out.print(0);
        }
    }
    @RequestMapping("findByCid")
    public ModelAndView findByCid(@RequestParam (value = "cid") int cid){
         Cake cake  = cakeService.findByCid(cid);
        ModelAndView mv = new ModelAndView("message");
        mv.addObject("cake", cake);
        return mv;
    }
}
